/**
 * 管理所有玩家的信息
 * 获取当前玩家
 * 记录当前玩家
 * 切换玩家
 *
 * 如何定义一个不知道初始值的变量
 * 1.可选类型
 * private var instance: KPlayerManager? = null
 * 2.延迟初始化
 * private lateinit var instance2:KPlayerManager
 */
class KPlayerManager private constructor(){
    //伴生对象
    //静态的属性和方法
    companion object{
        //静态属性 初始化一次
        //val INSTANCE = KPlayerManager()

        //记录创建好的对象
        private  var instance: KPlayerManager? = null

        private val obj = String()
        //给外部提供一个访问器方法
        fun sharedInstance(): KPlayerManager{
            if (instance == null){
                synchronized(obj){
                    if (instance == null) {
                        instance = KPlayerManager()
                    }
                }
            }
            return instance as KPlayerManager
        }
    }

    //保存所有玩家对象
    private var player1:KPlayer = KPlayer("玩家1",KChessType.BLACK)
    private var player2:KPlayer = KPlayer("玩家2",KChessType.WHITE)
    //保存当前玩家对象
    private var currentPlayer = player1

    //获取当前玩家
    fun getCurrentPlayer() = currentPlayer

    //切换玩家
    fun changePlayer(){
        currentPlayer = if (currentPlayer == player1){
            player2
        }else{
            player1
        }
    }
}












